package cn.schoolwow.workflow.test.module.gateway.testcase;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.check.instance.CheckInstance;
import cn.schoolwow.workflow.QuickWorkFlow;
import cn.schoolwow.workflow.domain.instance.StartWorkFlowRequest;
import cn.schoolwow.workflow.domain.instance.WorkFlowInstanceStatus;
import cn.schoolwow.workflow.domain.task.CompleteTaskRequest;
import cn.schoolwow.workflow.domain.task.TaskType;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskQuery;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskResponse;
import cn.schoolwow.workflow.entity.WorkFlowInstance;

public class StartInsuranceInclusiveGateWayAuditTestCaseFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        startWorkFlow(flowContext);
        submitIncident(flowContext);
        instanceAudit(flowContext);
        checkInstanceStatus(flowContext);
    }

    @Override
    public String name() {
        return "启动保险包含网关审批流程测试用例";
    }

    private void startWorkFlow(FlowContext flowContext){
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);

        StartWorkFlowRequest startWorkFlowRequest = new StartWorkFlowRequest();
        startWorkFlowRequest.definitionName = "保险理赔包容网关审批";
        startWorkFlowRequest.initiator = "张三";
        flowContext.putCurrentFlowInstanceData(startWorkFlowRequest);

        WorkFlowInstance workFlowInstance = quickWorkFlow.startWorkFlow(startWorkFlowRequest);
        flowContext.putInstanceData(workFlowInstance);
    }

    private void submitIncident(FlowContext flowContext){
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);
        StartWorkFlowRequest startWorkFlowRequest = flowContext.checkInstanceData(StartWorkFlowRequest.class);
        WorkFlowInstance workFlowInstance = flowContext.checkInstanceData(WorkFlowInstance.class);

        WorkFlowTaskQuery workFlowTaskQuery = new WorkFlowTaskQuery();
        workFlowTaskQuery.instanceIdList.add(workFlowInstance.getId());
        workFlowTaskQuery.assignerList.add(startWorkFlowRequest.initiator);
        flowContext.putCurrentFlowInstanceData(workFlowTaskQuery);

        WorkFlowTaskResponse workFlowTaskResponse = quickWorkFlow.getSingleWorkFlowTask(workFlowTaskQuery);
        CompleteTaskRequest completeTaskRequest = new CompleteTaskRequest();
        completeTaskRequest.taskId = workFlowTaskResponse.taskId;
        completeTaskRequest.taskType = TaskType.COMPLETE;
        completeTaskRequest.contextData.put("事故类型", new String[]{"车辆事故","自然灾害"});
        quickWorkFlow.completeTask(completeTaskRequest);
    }

    private void instanceAudit(FlowContext flowContext){
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);
        WorkFlowTaskQuery workFlowTaskQuery = flowContext.checkInstanceData(WorkFlowTaskQuery.class);

        String[] assigners = new String[]{"保险公司", "灾害管理部门"};
        for(String assigner:assigners){
            workFlowTaskQuery.assignerList.clear();
            workFlowTaskQuery.assignerList.add(assigner);
            WorkFlowTaskResponse workFlowTaskResponse = quickWorkFlow.getSingleWorkFlowTask(workFlowTaskQuery);
            quickWorkFlow.completeTask(workFlowTaskResponse.taskId, TaskType.COMPLETE);
        }
    }

    private void checkInstanceStatus(FlowContext flowContext){
        WorkFlowInstance workFlowInstance = flowContext.checkInstanceData(WorkFlowInstance.class);
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);

        WorkFlowInstance currentWorkFlowInstance = quickWorkFlow.getSingleWorkFlowInstance(workFlowInstance.getId());
        CheckInstance.newCheck().fieldValueCheck(currentWorkFlowInstance.getStatus(), WorkFlowInstanceStatus.Finished.status, "工作流实例状态");
    }

}
